package com.ruoyi.web.controller.erp.transport;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.transport.domian.TransportUnitType;
import com.ruoyi.erp.transport.service.TransportUnitTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * yun.wang
 * 运输单位类型
 */
@Api("运输单位类型")
@Controller
@RequestMapping("/transport")
public class TransportUnitTypeController extends BaseController {
    @Autowired
    TransportUnitTypeService transportUnitTypeService;
//    private String prefix = "erp/transport/index";

//    @PreAuthorize("@ss.hasPermi('transport:list:view')")
//    @GetMapping()
//    public String user()
//    {
//        return prefix + "/transport";
//    }

    @ApiOperation("运输单位类型列表")
    @PreAuthorize("@ss.hasPermi('transport:list:list')")
    @RequestMapping(value = "list",method = {RequestMethod.GET})
    @ResponseBody
    public TableDataInfo selectIsoUnitAll(
            @RequestParam(value = "weightCapacity",required = false) String weightCapacity,
            @RequestParam(value = "volumeCapacity",required = false) String volumeCapacity,
            @RequestParam(value = "transportUnitTypeId",required = false) String transportUnitTypeId,
            @RequestParam(value = "description",required = false) String description,
            @RequestParam(value = "uomForWeight",required = false) String uomForWeight,
            @RequestParam(value = "uomForVolume",required = false) String uomForVolume
    ){
        startPage();
        List<TransportUnitType> json =transportUnitTypeService.selectTransportUnitTypeAll(
                weightCapacity,
                volumeCapacity,
                transportUnitTypeId,
                description,
                uomForWeight,
                uomForVolume);
        return getDataTable(json);
    }


//    /**
//     * 新增运输单位类型
//     */
//    @GetMapping("/add")
//    public String add()
//    {
//        return prefix + "/add";
//    }

    /**
     * 新增保存运输单位类型
     */
    @PreAuthorize("@ss.hasPermi('transport:list:add')")
    @Log(title = "运输单位类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody @Validated TransportUnitType type)
    {
        if (UserConstants.NOT_UNIQUE.equals(transportUnitTypeService.checkTransportCodeUniqueAdd(type)))
        {
            return AjaxResult.error("新增运输单位类型'" + type.getTransportUnitTypeId() + "'失败，运输单位类型已存在");
        }
        type.setCreateBy(SecurityUtils.getUsername());
        return toAjax(transportUnitTypeService.insertTransport(type));
    }

//
//    /**
//     * 修改运输单位类型
//     */
//    @GetMapping("/edit/{id}")
//    public String edit(@PathVariable("id") String transportUnitTypeId, ModelMap mmap)
//    {
//        mmap.put("transport", transportUnitTypeService.selectTransportCode(transportUnitTypeId));
//        return prefix + "/edit";
//    }
    /**
     * 根据编号获取详细信息用于修改运输单位类型
     */
    @Log(title = "根据编号获取详细信息用于修改运输单位类型", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('transport:list:query')")
    @GetMapping(value = { "/getTransport/{id}" })
    @ResponseBody
    public AjaxResult getTransport(@PathVariable("id") String id)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(id))
        {
            ajax.put(AjaxResult.DATA_TAG, transportUnitTypeService.selectTransportCode(id));
        }
        return ajax;
    }

    /**
     * 修改保存运输单位类型
     */
    @PreAuthorize("@ss.hasPermi('transport:list:edit')")
    @Log(title = "运输单位类型", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody @Validated TransportUnitType type){
        if (UserConstants.NOT_UNIQUE.equals(transportUnitTypeService.checkTransportCodeUnique(type)))
        {
            return AjaxResult.error("运输单位类型'" + type.getTransportUnitTypeId() + "'失败，运输单位类型已存在");
        }
        type.setUpdateBy(SecurityUtils.getUsername());
//        Integer flag = transportUnitTypeService.insertOrupdateTransport(type);
//        if(flag ==98 ||flag == 99){
//            String str ="";
//            if(flag ==98){
//                str = "新增";
//            }else{
//                str = "修改";
//            }
//            return AjaxResult.success(str+"成功",flag);
//        }else{
//            return AjaxResult.error();
//        }
        return toAjax(transportUnitTypeService.updateTransport(type));
    }

//    /**
//     * 新增或修改保存运输单位类型
//     */
//    @PreAuthorize("@ss.hasPermi('transport:list:edit')")
//    @Log(title = "运输单位类型", businessType = BusinessType.UPDATE)
////    @PutMapping("/edit")
//    @PutMapping
//    @ResponseBody
//    public AjaxResult editSave(@RequestBody @Validated TransportUnitType type){
//        if (UserConstants.NOT_UNIQUE.equals(transportUnitTypeService.checkTransportCodeUnique(type)))
//        {
//            return AjaxResult.error("运输单位类型'" + type.getTransportUnitTypeId() + "'失败，运输单位类型已存在");
//        }
//        type.setUpdateBy(SecurityUtils.getUsername());
//        Integer flag = transportUnitTypeService.insertOrupdateTransport(type);
//        if(flag ==98 ||flag == 99){
//            String str ="";
//            if(flag ==98){
//                str = "新增";
//            }else{
//                str = "修改";
//            }
//            return AjaxResult.success(str+"成功",flag);
//        }else{
//            return AjaxResult.error();
//        }
//    }
    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<TransportUnitType> util = new ExcelUtil<TransportUnitType>(TransportUnitType.class);
        return util.importTemplateExcel("运输单位类型数据");
    }

    @Log(title = "运输单位类型", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('transport:list:import')")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TransportUnitType> util = new ExcelUtil<TransportUnitType>(TransportUnitType.class);
        List<TransportUnitType> transportList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = transportUnitTypeService.importTransport(transportList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @Log(title = "运输单位类型", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('transport:list:export')")
    @RequestMapping(value = {"/export","/export/{ids}"},method = {RequestMethod.GET})
    @ResponseBody
    public AjaxResult export(@PathVariable(value = "ids",required = false)String  ids)
    {
        List<TransportUnitType> list = transportUnitTypeService.selectTransportUnitList(ids);
        ExcelUtil<TransportUnitType> util = new ExcelUtil<TransportUnitType>(TransportUnitType.class);
        return util.exportExcel(list, "运输单位类型");
    }

    @PreAuthorize("@ss.hasPermi('transport:list:remove')")
    @Log(title = "运输单位类型", businessType = BusinessType.DELETE)
    @DeleteMapping(value = { "/remove/{ids}" })
    @ResponseBody
    public AjaxResult remove(@PathVariable("ids")String ids)
    {
        return toAjax(transportUnitTypeService.deleteTransportByIds(ids));
    }

}
